// pages/user/empower/upgradeRules/upgradeRules.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        applyType: 0,
        applyImg: ['', 'https://mozi.bailingyoupin.com/static/images/wx_sm/userCenter/shopKeer-rules-img.jpg', 'https://mozi.bailingyoupin.com/static/images/wx_sm/userCenter/service-rules-img.jpg']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            applyType: options.type
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    noAgree: function() {
        wx.showToast({
            title: '不同意将无法进行升级申请',
            icon: 'none',
            duration: 1000
        })
    },

    agree: function() {
        wx.showLoading({
            title: '请稍后',
        })
        const that = this
        let data
        if (wx.getStorageSync('belongId') == 0 || wx.getStorageSync('belongType') < 2) {
            wx.login({
                success(loginRes) {
                    wx.setStorageSync('code', loginRes.code)
                    data = {
                        code: loginRes.code,
                        type: that.data.applyType,
                        pay_client: 1
                    }
                    common.post('Quota/apply_up_role', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.navigateBack({
                                            delta: 1
                                        })
                                    }
                                }
                            })
                        } else {
                            wx.requestPayment({
                                timeStamp: res.timeStamp,
                                nonceStr: res.nonceStr,
                                package: res.package,
                                signType: res.signType,
                                paySign: res.paySign,
                                success(res) {
                                    wx.switchTab({
                                        url: '/pages/user/userCenter/userCenter'
                                    })
                                },
                                fail(res) {
                                    wx.showToast({
                                        title: '支付失败',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                }
                            })
                        }
                    }).catch(function(e) {})
                }
            })
        } else {
            data = {
                type: that.data.applyType
            }
            common.post('Quota/apply_up_role', data).then(function(res) {
                wx.hideLoading()
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: 1
                                })
                            }
                        }
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '申请成功，请耐心等候邀请人审核',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.switchTab({
                                    url: '/pages/user/userCenter/userCenter'
                                })
                            }
                        }
                    })
                }
            }).catch(function(e) {})
        }
    }
})